//
//  PostListViewModel.m
//  GLGW_Shoping
//
//  Created by whx on 2018/12/4.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "PostListViewModel.h"
#import "PostsTableViewCell.h"
#import "NoDataFootTableViewHeaderFooterView.h"
#import "CircleEditBottomView.h"

@interface PostListViewModel ()<UITableViewDelegate, UITableViewDataSource>
{
    UITableView *selfTableView;
    UIView *selfView;
}
@property (nonatomic ,strong) NSMutableArray *dataArray;
@property (nonatomic ,strong) CircleEditBottomView *bottomView;//底部视图


@end

@implementation PostListViewModel

- (instancetype)initWithViewToViewModel:(UITableView *)tableView theSuperView:(UIView *)superView
{
    if (self = [super initWithViewToViewModel:tableView theSuperView:superView]) {
        [self.dataArray addObjectsFromArray:@[@"1",@"1",@"1",@"2",@"2",@"2",@"3",@"3",@"3"]];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        tableView.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
        tableView.sectionFooterHeight = 0.0;
        tableView.sectionHeaderHeight = 0.0;
        [tableView registerClass:[NoDataFootTableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"NoDataFootTableViewHeaderFooterView"];
        selfTableView = tableView;
        selfView = superView;
        [superView addSubview:tableView];
        if (self.postType == PostType_LikePage) {
            tableView.height -= kHeightProportion_In_iPhoneTriploid(44);
        }
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stateChange:) name:@"EditAction" object:nil];
    }
    return self;
}

- (void)setPostType:(PostType)postType
{
    _postType = postType;
    if (_postType == PostType_Other) {
        selfTableView.height += kHeightProportion_In_iPhoneTriploid(44);
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (self.postType == PostType_LikePage) {
        return 0.01;
    }
    return kHeightProportion_In_iPhoneTriploid(10);
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    NoDataFootTableViewHeaderFooterView *footer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"NoDataFootTableViewHeaderFooterView"];
    return footer;
}

#pragma mark - tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kHeightProportion_In_iPhoneTriploid(132);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PostsTableViewCell *cell = [PostsTableViewCell cellWithTableView:tableView];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.editing) {
        
    } else {
        [tableView deselectRowAtIndexPath:indexPath animated:NO];
//        if (self.BlockSelect) {
//            self.BlockSelect(indexPath);
//        }
    }
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.editing) {
    }
    //        DLog(@"取消跳转");
}

#pragma mark -编辑
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert;
}

- (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath
{
    //    if (editingStyle == UITableViewCellEditingStyleDelete) {
    //        [self.dataArray removeObjectAtIndex: indexPath.row];
    //        [self.tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath]
    //                              withRowAnimation:UITableViewRowAnimationFade];
    //        [self.tableView reloadData];
    
    //    }
}

- (void)stateChange:(NSNotification *)notification
{
    NSArray * arr = notification.object;
    NSInteger index = [arr[0] integerValue];
    BOOL isSelect = [arr[1] boolValue];
    if (index == 1) {
        if (!isSelect) {
            [selfTableView setEditing:YES animated:YES];
            
            if (_bottomView.seleAll.selected) { // 改变文字
                _bottomView.seleAll.selected = !_bottomView.seleAll.selected;
                [_bottomView.seleAll setTitle:@"全选" forState:UIControlStateNormal];
            }
            [self.bottomView showViewHighe];
//            [UIView animateWithDuration:1 animations:^{
//                self->selfTableView.width += 46;
//            }];
        }else{
            
            if (!selfTableView.editing) {
                return;
            }
            [self setEditEnd];
        }
    } else {
        
        if (!selfTableView.editing) {
            return;
        }
        [self setEditEnd];
    }
}

- (void)setEditEnd
{
    [selfTableView setEditing:NO animated:YES];
    [self.bottomView dismissViewHighe];
//    selfTableView.width -= 46;
}

- (void)setEdit:(UIButton *)sender
{
    sender.selected = !sender.selected;
    if (sender.selected) {
        //点击编辑的时候清空删除数组
        [sender setTitle:@"完成" forState:UIControlStateNormal];
        sender.tintColor = [UIColor whiteColor];
        [sender setTitleColor:[UIColor colorWithHexString:@"#666666"] forState:UIControlStateSelected];
        [selfTableView setEditing:YES animated:YES];
        
        [self.bottomView showView];
        if (_bottomView.seleAll.selected) { // 改变文字
            _bottomView.seleAll.selected = !_bottomView.seleAll.selected;
            [_bottomView.seleAll setTitle:@"全选" forState:UIControlStateNormal];
        }
        
    } else {
        [sender setTitle:@"编辑" forState:UIControlStateNormal];
        [selfTableView setEditing:NO animated:YES];
        [self.bottomView dismissView];
    }
}

- (void)seletAll:(GLGWButton *)btn
{
    btn.selected = !btn.selected;
    
    if (btn.selected) {
        for (int i = 0; i< self.dataArray.count; i++) { // 全选实现方法
            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
            [selfTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
        }
        [btn setTitle:@"取消" forState:UIControlStateNormal];
    } else { // 取消选中
        NSArray *indexArr = [selfTableView indexPathsForSelectedRows];
        for (int i = 0; i < indexArr.count; i++) {
            NSIndexPath *indexPath = indexArr[i];
            [selfTableView deselectRowAtIndexPath:indexPath animated:NO];
        }
        [btn setTitle:@"全选" forState:UIControlStateNormal];
    }
}

// 删除数据方法
- (void)deleteData
{
    if (self.dataArray.count > 0) {
        NSArray *deleArr = [selfTableView indexPathsForSelectedRows];
        for (NSInteger i = deleArr.count - 1; i >= 0; i --) {
            NSIndexPath *index = deleArr[i];
            [self.dataArray removeObjectAtIndex:index.row];
        }
        [selfTableView reloadData];
    }
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        self.dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (CircleEditBottomView *)bottomView
{
    if (!_bottomView) {
        _bottomView = [[CircleEditBottomView alloc] initWithInSuperView:selfView];
        [_bottomView.seleAll addTarget:self action:@selector(seletAll:) forControlEvents:UIControlEventTouchUpInside];
        kWeakSelf(self);
        _bottomView.BlockAllDelete = ^(GLGWButton * _Nullable deleteBtn) {
            [weakself deleteData];
        };
    }
    return _bottomView;
}

@end
